package io.github.stary1993.kite.admin.controller;

import io.github.stary1993.kite.core.util.R;
import io.github.stary1993.kite.quartz.constants.KiteQuartzEnum;
import io.github.stary1993.kite.quartz.constants.QuartzJobTypeEnum;
import io.github.stary1993.kite.quartz.core.QuartzJob;
import io.github.stary1993.kite.quartz.support.QuartzTemplate;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "quartz任务模块")
@RequiredArgsConstructor
@RequestMapping("/quartz")
@RestController
public class QuartzController {

    private final QuartzTemplate quartzTemplate;

    @Operation(summary = "添加更新任务")
    @Parameter(name = "quartzJob", description = "任务信息", required = true)
    @PostMapping("/addOrUpdateJob")
    public R addOrUpdateJob(@RequestBody QuartzJob quartzJob) {
        quartzJob.setJobName("demo");
        quartzJob.setJobGroup("demo");
        quartzJob.setJobOrder("1");
        quartzJob.setJobType(QuartzJobTypeEnum.SPRING_BEAN);
        quartzJob.setClassName("demo");
        quartzJob.setMethodName("demoMethod");
        quartzJob.setMethodParamsValue("参数666");
        quartzJob.setCronExpression("0/5 * * * * ?");
        quartzJob.setMisfirePolicy(KiteQuartzEnum.MISFIRE_IGNORE_MISFIRES.getType());
        quartzTemplate.addOrUpdateJob(quartzJob);
        return R.ok();
    }

    @Operation(summary = "暂停任务")
    @Parameter(name = "quartzJob", description = "任务信息", required = true)
    @PostMapping("/pauseJob")
    public R pauseJob(@RequestBody QuartzJob quartzJob) {
        quartzJob.setJobName("demo");
        quartzJob.setJobGroup("demo");
        quartzTemplate.pauseJob(quartzJob);
        return R.ok();
    }

    @Operation(summary = "恢复任务")
    @Parameter(name = "quartzJob", description = "任务信息", required = true)
    @PostMapping("/resumeJob")
    public R resumeJob(@RequestBody QuartzJob quartzJob) {
        quartzJob.setJobName("demo");
        quartzJob.setJobGroup("demo");
        quartzTemplate.resumeJob(quartzJob);
        return R.ok();
    }

    @Operation(summary = "删除任务")
    @Parameter(name = "quartzJob", description = "任务信息", required = true)
    @PostMapping("/removeJob")
    public R removeJob(@RequestBody QuartzJob quartzJob) {
        quartzJob.setJobName("demo");
        quartzJob.setJobGroup("demo");
        quartzTemplate.removeJob(quartzJob);
        return R.ok();
    }

}
